package com.czl.module_storehouse.activity.remand.damage;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.FrameLayout;

import com.blankj.utilcode.util.KeyboardUtils;
import com.czl.base.data.bean.tengyun.RecordRemandBean;
import com.czl.base.data.bean.tengyun.SortBean;
import com.czl.module_base.activity.BaseRecyclerViewActivity;
import com.czl.module_base.bean.ListBean;
import com.czl.module_base.mvp.presenter.InjectPresenter;
import com.czl.module_storehouse.R;
import com.czl.module_storehouse.adapter.DirectDamageSearchAdapter;
import com.czl.module_storehouse.databinding.ActivityDirectDamageSearchBinding;
import com.czl.module_storehouse.event.DamageRegisPostEvent;
import com.czl.module_storehouse.event.DamageRegisSearchEvent;
import com.czl.module_storehouse.mvp.presenter.RemandHomePresenter;
import com.czl.module_storehouse.mvp.view.RemandHomeView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

/**
 * @author xubin
 */
public class DirectDamageSearchActivity extends BaseRecyclerViewActivity<ActivityDirectDamageSearchBinding> implements RemandHomeView {

    @InjectPresenter
    RemandHomePresenter mRemandHomePresenter;

    private DirectDamageSearchAdapter mAdapter;
    private boolean loading;

    @Override
    protected ActivityDirectDamageSearchBinding getChildBinding(LayoutInflater layoutInflater, FrameLayout containerTop) {
        return ActivityDirectDamageSearchBinding.inflate(layoutInflater, containerTop, true);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        mToolBinding.toolbarContentTitle.setText("损坏登记");
        mChildBinding.viewSearch.etSearch.setHint(R.string.enter_name_or_tem_name);
        mAdapter = new DirectDamageSearchAdapter(new ArrayList<>());
        binding.recyclerView.setAdapter(mAdapter);
        binding.smartRefreshLayout.setEnableLoadMore(false);
        binding.smartRefreshLayout.setEnableRefresh(false);
        EventBus.getDefault().register(this);
        initListener();
    }

    protected void initListener() {
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            RecordRemandBean item = mAdapter.getItem(position);
            if (item.getItemType() == RecordRemandBean.VIEW_TYPE_CHILDREN) {
                nextDamageActivity(item.getSortBean());
            }
        });
        mChildBinding.viewSearch.ivSearch.setOnClickListener(v -> getProUseInfo(true));
        mChildBinding.textReset.setOnClickListener(v -> reset());
    }

    private void nextDamageActivity(SortBean bean) {
        EventBus.getDefault().postSticky(new DamageRegisSearchEvent(bean));
        startActivity(new Intent(getContext(), DamageRegisProductActivity.class));
    }

    @Subscribe(threadMode = ThreadMode.POSTING, sticky = true)
    public void onDataEvent(DamageRegisPostEvent event) {
        finish();
    }

    private void reset() {
        mChildBinding.etName.setText("");
        mChildBinding.etSortName.setText("");
        mChildBinding.viewSearch.etSearch.setText("");
        mAdapter.setList(new ArrayList<>());
    }

    @Override
    protected void loadMore() {
        super.loadMore();
        getProUseInfo(false);
    }

    private void getProUseInfo(boolean refresh) {
        KeyboardUtils.hideSoftInput(this);
        if (mRemandHomePresenter != null) {
//            mPresenter.getProUseInfo(etName.getText().toString(), etSortName.getText().toString());
            mRemandHomePresenter.getProUseInfo(refresh, mChildBinding.viewSearch.etSearch.getText().toString(), loading);
        }
    }

    @Override
    public void hideDialogLoading() {
        super.hideDialogLoading();
        loading = true;
    }

    @Override
    public <T> void showData(T t) {
        super.showData(t);
        if (t == null) {
            showEmpty();
            return;
        }
        ListBean<RecordRemandBean> listBean = (ListBean<RecordRemandBean>) t;
        if (listBean.getList() == null || listBean.getList().isEmpty()) {
            showEmpty();
            return;
        }
        mAdapter.replaceList(listBean.getList());
    }
}
